package cn.com.fafa.util;

import org.springframework.util.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by ZhouBing on 2017/7/19.
 */
public class DateUtils {


    //定义日期模板
    private static String defaultDatePattern = "yyyy-MM-dd";
    public final static String DEFAULT_DATE_FORMAT = "yyyy年MM月dd日";
    public final static String DATETIME_FORMAT_HHMMSS = "yyyy年MM月dd日 HH时mm分ss秒";
    public final static String yyMMdd = "yy-MM-dd"; //150924
    public final static String yyyyMMdd = "yyyy-MM-dd"; //2015-09-24
    public final static String HHmmss = "HH:mm:ss"; //22:19:20
    public final static String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss"; //2015-09-24 22:19:20
    public final static String yyMMddHHmmss = "yy-MM-dd HH:mm:ss"; //15-09-24 22:19:20
    public final static String yyMMddHHmmssSS = "yy-MM-dd HH:mm:ss.SSS"; //15-09-24 22:19:20.160
    // 短日期格式
    public static String DATE_FORMAT = "yyyy-MM-dd";

    // 长日期格式
    public static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static SimpleDateFormat sdf;


    /**
     * Date日期转换为字符串
     * @param date
     * @return
     */
    public static String dateToStr(Date date){

        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        return sdf.format(date);
    }

    /**
     * 自定义格式 Date日期转换为字符串
     * @param date
     * @param pattern
     * @return
     */
    public static String dateToStr(Date date, String pattern){

        sdf = new SimpleDateFormat(pattern);

        return sdf.format(date);
    }


    /**
     * 字符串转换为日期
     * @param dateStr
     * @return
     */
    public static Date strToDate(String dateStr){

        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串转换为日期--自定义
     * @param dateStr
     * @param pattern
     * @return
     */
    public static Date myStrToDate(String dateStr, String pattern){

        sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将日期格式的字符串转换为长整型
     *
     * @param date
     * @param format
     * @return
     */
    public static long strToLong(String date, String format) {
        try {
            if (!StringUtils.isEmpty(date)) {
                if (StringUtils.isEmpty(format))
                    format = DateUtils.TIME_FORMAT;
                SimpleDateFormat sf = new SimpleDateFormat(format);
                return sf.parse(date).getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0l;
    }

    /**
     * 将长整型数字转换为日期格式的字符串
     * @param time
     * @param format
     * @return
     */
    public static String longToStr(long time, String format) {
        if (time > 0l) {
            if (StringUtils.isEmpty(format))
                format = DateUtils.TIME_FORMAT;
            SimpleDateFormat sf = new SimpleDateFormat(format);
            Date date = new Date(time);
            return sf.format(date);
        }
        return "";
    }


    /**
     * 将指定日期转换为默认格式yyyy-MM-dd
     * @param date
     * @return
     */
    public static String dateToStrByDefaultPattern(Date date) {
        return new SimpleDateFormat(defaultDatePattern).format(date);
    }

    /**
     * 将指定日期转换为自定义格式
     * @param date
     * @param pattern
     * @return
     */
    public static String dateToStrByCustomPattern(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }






    /**
     * 获得当前时间字符串 格式  yyyy-MM-dd HH:mm:ss.SSS  2018-08-15 11:49:05.169
     * @return
     */
    public static String getCurDateStr(){

//        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return  sdf.format(new Date());
    }
    /**
     * 获得当前时间字符串 格式
     * @return
     */
    public static String getCurDateStr(String partner){

        sdf = new SimpleDateFormat(partner);

        return  sdf.format(new Date());
    }


    /**
     * 获取当前系统的日期--long型
     * @return
     */
    public static long getCurDateLongTime() {
        return System.currentTimeMillis();
    }

    public static long getDateLongTime(Date date) {
        return date.getTime();
    }
    public static long getStrLongTime(String  time) {
        Date date1 = null;
        try {
            date1 = new SimpleDateFormat("yyyy-MM-dd").parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long Timestamp = date1.getTime();
        return Timestamp;
    }

    /**
     * 返回自定义格式的当前日期
     * @param pattern 指定的格式
     * @return 指定格式的日期
     */
    public static String getCurTimeMyPattern(String pattern) {
        return new SimpleDateFormat(pattern).format(new Date());
    }

    /**
     * 返回当前日期 --Wed Aug 15 11:49:05 CST 2018
     * @return
     */
    public static Date getCurTime() {
        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }

    /**
     * 返回当前年份
     * @return
     */
    public static int getCurYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 返回当前月份
     * @return
     */
    public static int getCurMonth() {
        Calendar calendar = Calendar.getInstance();
        return (calendar.get(Calendar.MONTH) + 1);
    }

    /**
     * 返回当前日
     * @return
     */
    public static int getCurDay() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DATE);
    }



    //判断选择的日期是否是本周
    public static boolean isThisWeek(long time)
    {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(time));
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if(paramWeek==currentWeek){
            return true;
        }
        return false;
    }
    //判断选择的日期是否是今天
    public static boolean isToday(long time)
    {
        return isThisTime(time,"yyyy-MM-dd");
    }
    //判断选择的日期是否是本月
    public static boolean isThisMonth(long time)
    {
        return isThisTime(time,"yyyy-MM");
    }
    //判断选择的日期是否是本年
    public static boolean isThisYear(long time)
    {
        return isThisTime(time,"yyyy");
    }
    private static boolean isThisTime(long time,String pattern) {
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String param = sdf.format(date);//参数时间
        String now = sdf.format(new Date());//当前时间
        if(param.equals(now)){
            return true;
        }
        return false;
    }

/**
 * 以下是开始时间，结束时间
 */
//获取当天的开始时间
public static java.util.Date getDayBegin() {
    Calendar cal = new GregorianCalendar();
    cal.set(Calendar.HOUR_OF_DAY, 0);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    return cal.getTime();
}
    //获取当天的结束时间
    public static java.util.Date getDayEnd() {
        Calendar cal = new GregorianCalendar();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
    //获取昨天的开始时间
    public static Date getBeginDayOfYesterday() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    //获取昨天的结束时间
    public static Date getEndDayOfYesterDay() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }
    //获取明天的开始时间
    public static Date getBeginDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayBegin());
        cal.add(Calendar.DAY_OF_MONTH, 1);

        return cal.getTime();
    }
    //获取明天的结束时间
    public static Date getEndDayOfTomorrow() {
        Calendar cal = new GregorianCalendar();
        cal.setTime(getDayEnd());
        cal.add(Calendar.DAY_OF_MONTH, 1);
        return cal.getTime();
    }
    //获取本周的开始时间
    @SuppressWarnings("unused")
    public static Date getBeginDayOfWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek);
        return getDayStartTime(cal.getTime());
    }
    //获取本周的结束时间
    public static Date getEndDayOfWeek(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }
    //获取上周的开始时间
    @SuppressWarnings("unused")
    public static Date getBeginDayOfLastWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return getDayStartTime(cal.getTime());
    }
    //获取上周的结束时间
    public static Date getEndDayOfLastWeek(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfLastWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);
    }
    //获取本月的开始时间
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getCurYear(), getCurMonth() - 1, 1);
        return getDayStartTime(calendar.getTime());
    }
    //获取本月的结束时间
    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getCurYear(), getCurMonth() - 1, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getCurYear(), getCurMonth() - 1, day);
        return getDayEndTime(calendar.getTime());
    }
    //获取上月的开始时间
    public static Date getBeginDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getCurYear(), getCurMonth() - 2, 1);
        return getDayStartTime(calendar.getTime());
    }
    //获取上月的结束时间
    public static Date getEndDayOfLastMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(getCurYear(), getCurMonth() - 2, 1);
        int day = calendar.getActualMaximum(5);
        calendar.set(getCurYear(), getCurMonth() - 2, day);
        return getDayEndTime(calendar.getTime());
    }
    //获取本年的开始时间
    public static java.util.Date getBeginDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getCurYear());
        // cal.set
        cal.set(Calendar.MONTH, Calendar.JANUARY);
        cal.set(Calendar.DATE, 1);

        return getDayStartTime(cal.getTime());
    }
    //获取本年的结束时间
    public static java.util.Date getEndDayOfYear() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, getCurYear());
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DATE, 31);
        return getDayEndTime(cal.getTime());
    }
    //获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }
    //获取某个日期的结束时间
    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) calendar.setTime(d);
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),    calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }




    //获取两个日期中的最小日期
    public static Date min(Date beginDate, Date endDate) {
        if (beginDate == null) {
            return endDate;
        }
        if (endDate == null) {
            return beginDate;
        }
        if (beginDate.after(endDate)) {
            return endDate;
        }
        return beginDate;
    }
}
